
import {getRequest} from "./api";
import router from "../router";
export const initMenu=(router,store)=>{
    //看路由的数组中是否存在元素，存在则直接return
    if(store.state.routes.length> 0){
        return;
    }
    //router数组中的元素为空，进行请求，获取菜单
    getRequest("/system/config/menu").then(data=>{
        if(data){
            let fmtRoutes = formatRoutes(data);
            router.addRoutes(fmtRoutes);
            store.commit('initRoutes',fmtRoutes);//向vuex中提交数据
        }
    })
}

export const formatRoutes=(routes)=>{
    let fmRoutes = [];
    //遍历roures数组中的值
    routes.forEach(router=>{
        //批量定义 let path = router.path
        let{
            path,
            component,
            name,
            meta,
            iconCls,
            children
        }=router;
        if(children && children instanceof Array){//一级菜单
            children = formatRoutes(children);//递归调用
        }
        let fmRouter = {
            path:path,
            name:name,
            iconCls:iconCls,
            meta:meta,
            children:children, //处理之后的children
            component(resolve){
                require(['../views/'+component+'.vue'],resolve);//动态导入
            }
        }
        fmRoutes.push(fmRouter);
    })
    return fmRoutes;
}
















